package com.ruoyi.common.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SMSUtils {

    public static void sendMessage(String phoneNum, String code) throws Exception {
        Client client = SMSUtils.createClient("LTAI4GBNVhJgQXMwQjwqNUsY", "v8aTmMtrTfZSgmgx0yXu4UIlYycmiQ");
        SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(
                new SendSmsRequest().setPhoneNumbers(phoneNum).setSignName("容易查").setTemplateCode("SMS_205879161").setTemplateParam("{\"code\":\"" + code + "\"}"),
                new RuntimeOptions()
        );
        log.warn("向" + phoneNum + "发送验证码响应：" + sendSmsResponse.getBody().getMessage());
    }

    public static void sendReport(String phoneNum, String path, String code) {
        try {
            Client client = SMSUtils.createClient("LTAI4GBNVhJgQXMwQjwqNUsY", "v8aTmMtrTfZSgmgx0yXu4UIlYycmiQ");
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(
                    new SendSmsRequest().setPhoneNumbers(phoneNum).setSignName("天启数科").setTemplateCode("SMS_474285277").setTemplateParam("{\"name\":\"" + path + "\",\"code\":\"" + code + "\"}"),
                    new RuntimeOptions()
            );
            log.warn("向" + phoneNum + "发送报告响应：" + sendSmsResponse.getBody().getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

}

